﻿using CloudDcPc.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CloudDcPc.FuckDlg
{
    /// <summary>
    /// MenuEditDlg.xaml 的交互逻辑
    /// </summary>
    public partial class MyMenuEditDlg : Window
    {       
        MyMenu myMenu = null;
        public MyMenuEditDlg(MyMenu myMenu)
        {
            InitializeComponent();
            this.myMenu = myMenu;
            main_grid.DataContext = myMenu;
            try
            {
                string[] arr_food_unit = Properties.Settings.Default.save_food_unit.Split('-');
                cb_food_unit.ItemsSource = arr_food_unit;
                if (arr_food_unit.Length == 0)
                    cb_food_unit.Visibility = Visibility.Collapsed;
            }
            catch
            {
                MessageBox.Show("设置中菜品单位配置有误！");
                cb_food_unit.Visibility = Visibility.Collapsed;
            }
            try
            {
                string[] arr_food_type = Properties.Settings.Default.save_food_type.Split('-');
                cb_food_type.ItemsSource = arr_food_type;
                if (arr_food_type.Length == 0)
                    cb_food_type.Visibility = Visibility.Collapsed;
            }
            catch
            {
                MessageBox.Show("设置中菜品类别配置有误！");
                cb_food_unit.Visibility = Visibility.Collapsed;
            }

            try
            {
                string[] arr_suffix = Properties.Settings.Default.save_food_suffix.Split('-');
                cb_suffix.ItemsSource = arr_suffix;
                if (arr_suffix.Length == 0)
                    cb_suffix.Visibility = Visibility.Collapsed;
            }
            catch
            {
                MessageBox.Show("设置中菜品后缀配置有误！");
                cb_suffix.Visibility = Visibility.Collapsed;
            }

            cb_xz.ItemsSource = App.MenuTypeBean.GetMenuTypes();
            //cb_xz.SelectedValue = myMenu.TextMenuType;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (txt_food_name.Text == "" || txt_food_num.Text == "" || txt_price.Text == "" || txt_type.Text == "" || txt_unit.Text == "" || txt_position.Text == "")
            {
                MessageBox.Show("请将必要的信息填写完整！(口味选填)");
                return;
            }
            try
            {
                float.Parse(txt_price.Text);
                int.Parse(txt_position.Text);
            }
            catch
            {
                MessageBox.Show("位置或价格填写错误！只能是数字！");
                return;
            }

            BindingExpression bind = txt_food_num.GetBindingExpression(TextBox.TextProperty);
            bind.UpdateSource();
            bind = txt_food_name.GetBindingExpression(TextBox.TextProperty);
            bind.UpdateSource();
            bind = txt_suffix.GetBindingExpression(TextBox.TextProperty);
            bind.UpdateSource();
            bind = txt_price.GetBindingExpression(TextBox.TextProperty);
            bind.UpdateSource();
            bind = txt_taste.GetBindingExpression(TextBox.TextProperty);
            bind.UpdateSource();
            bind = txt_type.GetBindingExpression(TextBox.TextProperty);
            bind.UpdateSource();
            bind = txt_unit.GetBindingExpression(TextBox.TextProperty);
            bind.UpdateSource();
            bind = cb_text_enough.GetBindingExpression(ComboBox.TextProperty);
            bind.UpdateSource();
            bind = cb_textprint_pos.GetBindingExpression(ComboBox.TextProperty);
            bind.UpdateSource();
            bind = txt_position.GetBindingExpression(TextBox.TextProperty);
            bind.UpdateSource();
            bind = cb_xz.GetBindingExpression(ComboBox.TextProperty);
            bind.UpdateSource();

            if (App.MyMenuBean.HasMyMenuByObj(myMenu))
            {
                MessageBox.Show("已经存在相同的菜编号！");
                return;
            }
           // myMenu.Suffix = cb_suffix.SelectedValue.ToString();
            myMenu.MenuType = ((MenuType)cb_xz.SelectedItem).Id;
           // MessageBox.Show(myMenu.Suffix);
            if (App.MyMenuBean.UpdateMyMenu(myMenu))
            {
                MessageBox.Show("更新成功！");
                this.Close();
            }
            else
            {
                MessageBox.Show("更新失败！");
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            string messageBoxText = "确认删除该菜品吗？";
            string caption = "删除菜品";
            MessageBoxButton button = MessageBoxButton.YesNo;
            MessageBoxImage icon = MessageBoxImage.Warning;
            MessageBoxResult result = MessageBox.Show(messageBoxText, caption, button, icon);
            if (result == MessageBoxResult.Yes)
            {
                if (App.MyMenuBean.DelMyMenuById(myMenu.Id))
                {
                    MessageBox.Show("删除成功！");
                    //this.DialogResult = true;
                    this.Close();
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }
            }
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void cb_food_unit_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            txt_unit.Text = cb_food_unit.SelectedValue + "";
        }

        private void cb_food_type_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            txt_type.Text = cb_food_type.SelectedValue + "";
        }

        private void cb_suffix_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            txt_suffix.Text = cb_suffix.SelectedValue + "";
        }
    }
    
}
